<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>编辑商品分类</title>
	<#include "/common/vue_resource.ftl">
	<#include "/common/upload.ftl">
</head>
<body>
<div id="app" v-cloak>
	<div class="ui-form">
		<form class="layui-form" @submit.prevent="submitForm()" method="post">
			<div class="layui-form-item">
				<label class="layui-form-label">分类名称<span class="ui-request">*</span></label>
				<div class="layui-input-block">
					<input type="text" v-model="record.name" placeholder="请输入名称" class="layui-input"/>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">上级分类</label>
				<div class="layui-input-block">
					<select v-model="record.parentId" class="layui-input">
						<option value="1">无上级分类</option>
						<option :value="item.id" v-for="(item, index) in topList">{{item.name}}</option>
					</select>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">商品单位</label>
				<div class="layui-input-block">
					<input type="text" v-model="record.productUnit" placeholder="请输入商品单位" class="layui-input"/>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">排序</label>
				<div class="layui-input-block">
					<input type="text" v-model="record.sort" placeholder="请输入排序" class="layui-input"/>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">分类显示</label>
				<div class="layui-input-block">
					<select v-model="record.navStatus" class="layui-input">
						<option value="2">不显示</option>
						<option value="1">显示</option>
					</select>
					<div class="layui-form-mid layui-word-aux">在移动端分类列表是否显示</div>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">导航栏</label>
				<div class="layui-input-block">
					<select v-model="record.showStatus" class="layui-input">
						<option value="2">不显示</option>
						<option value="1">显示</option>
					</select>
					<div class="layui-form-mid layui-word-aux">在移动端导航栏是否显示</div>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">图标</label>
				<div class="layui-input-block">
					<div style="display:inline-block;vertical-align:top;" v-if="record.icon">
						<img :src="record.iconFullPath" style="max-height:150px;max-width:150px;">
					</div>
					<div style="display:inline-block;vertical-align:top;">
						<a id="select-file-button">选择文件</a>
						<div class="layui-form-mid layui-word-aux">只能上传jpg/png文件，且不超过10MB</div>
					</div>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">关键字</label>
				<div class="layui-input-block">
					<input type="text" v-model="record.keywords" placeholder="请输入关键字" class="layui-input"/>
				</div>
			</div>

			<div class="layui-form-item">
				<div class="layui-input-block">
					<input type="submit" value="保存" class="layui-btn" />
				</div>
			</div>
		</form>
	</div>
</div>
<script>
	var app = new Vue({
		el: '#app',
		data: {
			showTypes: false,
			record : {
				id:'${params.id!}',
				parentId:'1',
				name:'',
				level:'',
				productUnit:'',
				navStatus:'2',
				showStatus:'2',
				sort:'0',
				icon:'',
				iconFullPath:'',
				keywords:'',
			},
			topList:[],
		},
		mounted: function () {
			this.init();
			this.loadData();
		},
		methods: {
			init:function(){
				var that = this;
				$.upload({
					accept:{title:'Images',extensions:'jpg,jpeg,png',mimeTypes:'image/!*'},
					fileSingleSizeLimit: 10 * 1024 * 1024,//10M
					uploadSuccess:function (file, data) {
						if (!data.success) {
							$.message(data.message);
							return;
						}
						var result = data.data[0];
						that.record.iconFullPath = "${params.fileRequestUrl!}" + result.path;
						that.record.icon = result.path;
					}
				});

				this.loadTopList();
			},
			loadTopList: function () {
				var that = this;
				$.http.post("${params.contextPath}/web/pmsProductCategory/queryTopList.json").then(function (data) {
					if (!data.success) {
						$.message(data.message);
						return;
					}
					that.topList = data.data;
					that.loadData();
				});
			},
			loadData: function () {
				if (!'${params.id!}') {
					return;
				}
				var that = this;
				$.http.post('${params.contextPath}/web/pmsProductCategory/query.json', {id: '${params.id!}'}).then(function (data) {
					if (!data.success) {
						$.message(data.message);
						return;
					}
					var item = data.data;
					for (var key in  that.record) {
						that.record[key] = item[key];
					}

					if (item.icon) {
						that.record.iconFullPath = "${params.fileRequestUrl!}" + item.icon;
					}
				});
			},
			submitForm: function () {
				$.http.post('${params.contextPath}/web/pmsProductCategory/<#if (params.id)??>modify<#else>save</#if>.json', this.record).then(function (data) {
					if (!data.success) {
						$.message(data.message);
						return;
					}
					var alt = layer.alert(data.message || "操作成功", function () {
						parent.app.loadData();
						parent.layer.closeAll();
						layer.close(alt);
					});
				});
			},
		}
	});
</script>
</body>

</html>
